package com.hc.tenant.service.impl;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hc.common.core.web.domain.AjaxResult;
import com.hc.common.security.service.TokenService;
import com.hc.tenant.domain.MqttMsgSend;
import com.hc.tenant.service.IMqttMsgSendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hc.tenant.mapper.RobotSettingMapper;
import com.hc.tenant.domain.RobotSetting;
import com.hc.tenant.service.IRobotSettingService;

/**
 * 机器人设置Service业务层处理
 * 
 * @author yongjianz
 * @date 2021-03-27
 */
@Service
public class RobotSettingServiceImpl implements IRobotSettingService 
{
    @Autowired
    private RobotSettingMapper robotSettingMapper;

    @Autowired
    private IMqttMsgSendService mqttMsgSendService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询机器人设置
     * 
     * @param robotId 机器人设置ID
     * @return 机器人设置
     */
    @Override
    public RobotSetting selectRobotSettingById(Long robotId)
    {
        return robotSettingMapper.selectRobotSettingById(robotId);
    }

    /**
     * 查询机器人设置列表
     * 
     * @param robotSetting 机器人设置
     * @return 机器人设置
     */
    @Override
    public List<RobotSetting> selectRobotSettingList(RobotSetting robotSetting)
    {
        return robotSettingMapper.selectRobotSettingList(robotSetting);
    }

    /**
     * 新增机器人设置
     * 
     * @param robotSetting 机器人设置
     * @return 结果
     */
    @Override
    public AjaxResult insertRobotSetting(RobotSetting robotSetting)
    {
        if(existUserId(robotSetting.getUserId(),null)){
            return AjaxResult.error("账号已关联");
        }
        int count = robotSettingMapper.insertRobotSetting(robotSetting);
        if(count>0){
            insertMqttMsgSend(robotSetting);
            return AjaxResult.success();
        }
        else
            return AjaxResult.error();
    }

    private boolean existUserId(Long userId, Long robotId) {
        RobotSetting robotSetting = new RobotSetting();
        robotSetting.setUserId(userId);
        List<RobotSetting> list = selectRobotSettingList(robotSetting);
        if(robotId == null){
            return list.size()>0;
        }else{
            return list.size()>0 && !robotId.equals(list.get(0).getRobotId());
        }

    }

    /**
     * 修改机器人设置
     * 
     * @param robotSetting 机器人设置
     * @return 结果
     */
    @Override
    public AjaxResult updateRobotSetting(RobotSetting robotSetting)
    {
        {
            if(existUserId(robotSetting.getUserId(),robotSetting.getRobotId())){
                return AjaxResult.error("账号已关联");
            }
            int count = robotSettingMapper.updateRobotSetting(robotSetting);
            if(count>0) {
                insertMqttMsgSend(robotSetting);
                return AjaxResult.success();
            }
            else
                return AjaxResult.error();
        }
    }

    /**
     * 批量删除机器人设置
     * 
     * @param robotIds 需要删除的机器人设置ID
     * @return 结果
     */
    @Override
    public int deleteRobotSettingByIds(Long[] robotIds)
    {
        return robotSettingMapper.deleteRobotSettingByIds(robotIds);
    }

    /**
     * 删除机器人设置信息
     * 
     * @param robotId 机器人设置ID
     * @return 结果
     */
    @Override
    public int deleteRobotSettingById(Long robotId)
    {
        return robotSettingMapper.deleteRobotSettingById(robotId);
    }

    private void insertMqttMsgSend(RobotSetting robotSetting){
        String userToken = tokenService.getUserToken(robotSetting.getUserId());
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter("announcer", "robotName", "voiceControl");
        String jsonString = JSON.toJSONString(robotSetting, filter);
        MqttMsgSend mqttMsgSend = new MqttMsgSend();
        mqttMsgSend.setCreateTime(new Date());
        mqttMsgSend.setMsg(jsonString);
        mqttMsgSend.setTopicName("saas/robotSettingChange/"+userToken);
        mqttMsgSendService.insertMqttMsgSend(mqttMsgSend);
    }
}
